
import { MockMethod } from 'vite-plugin-mock'
import { ContestVO } from '@/api/contest/types'

// 模拟数据
const contestList: ContestVO[] = [
  {
    id: 1,
    contestName: '2023编程大赛',
    contestType: '1',
    startTime: '2023-06-01 00:00:00',
    endTime: '2023-06-30 23:59:59',
    location: '北京',
    organizer: '中国计算机学会',
    status: '2',
    description: '年度编程大赛，面向全国开发者',
    maxParticipants: 1000,
    teamCount: 85,
    submissionCount: 76,
    reviewRound: 2,
    reviewProgress: 60,
    createTime: '2023-05-01 10:00:00',
    groups: [
      {
        name: '研究生组',
        stage: 'final',
        stageStart: '2023-06-01 00:00:00',
        stageEnd: '2023-06-30 23:59:59',
        status: '2',
        teamCount: 50,
        submissionCount: 45,
        reviewRound: 2,
        reviewProgress: 60
      },
      {
        name: '本科生组',
        stage: 'final',
        stageStart: '2023-06-01 00:00:00',
        stageEnd: '2023-06-30 23:59:59',
        status: '2',
        teamCount: 35,
        submissionCount: 31,
        reviewRound: 2,
        reviewProgress: 60
      }
    ]
  },
  {
    id: 2,
    contestName: '2023设计大赛',
    contestType: '2',
    startTime: '2023-07-01 00:00:00',
    endTime: '2023-07-31 23:59:59',
    location: '上海',
    organizer: '中国设计师协会',
    status: '1',
    description: '年度设计大赛，面向全国设计师',
    maxParticipants: 500,
    teamCount: 120,
    submissionCount: 95,
    reviewRound: 1,
    reviewProgress: 30,
    createTime: '2023-06-01 10:00:00',
    groups: [
      {
        name: '研究生组',
        stage: 'semifinal',
        stageStart: '2023-07-01 00:00:00',
        stageEnd: '2023-07-31 23:59:59',
        status: '1',
        teamCount: 70,
        submissionCount: 65,
        reviewRound: 1,
        reviewProgress: 30
      },
      {
        name: '本科生组',
        stage: 'semifinal',
        stageStart: '2023-07-01 00:00:00',
        stageEnd: '2023-07-31 23:59:59',
        status: '1',
        teamCount: 50,
        submissionCount: 30,
        reviewRound: 1,
        reviewProgress: 30
      }
    ]
  }
]

export default [
  // 查询大赛列表
  {
    url: '/competition/list',
    method: 'get',
    response: (req) => {
      const { contestName, contestType, status, pageNum = 1, pageSize = 10 } = req.query
      const filteredList = contestList.filter(item => {
        return (!contestName || item.contestName.includes(contestName)) &&
          (!contestType || item.contestType === contestType) &&
          (!status || item.status === status)
      })

      // 分页处理
      const start = (pageNum - 1) * pageSize
      const end = start + Number(pageSize)
      const paginatedList = filteredList.slice(start, end)

      return {
        code: 200,
        message: 'success',
        data: {
          rows: paginatedList.map(item => ({
            ...item,
            id: item.id // 确保返回的数据中包含id字段
          })),
          total: filteredList.length
        }
      }
    }
  },
  // 查询大赛详情
  {
    url: '/competition/detail',
    method: 'get',
    response: (req) => {
      const { id } = req.query
      const contest = contestList.find(item => item.id === id)
      return {
        code: 200,
        message: contest ? 'success' : '大赛不存在',
        data: contest || null
      }
    }
  },
  // 新增大赛
  {
    url: '/dev-api/competition/create',
    method: 'post',
    response: (req) => {
      const newContest = {
        ...req.body,
        id: `${contestList.length + 1}`,
        createTime: new Date().toISOString()
      }
      contestList.push(newContest)
      return {
        code: 200,
        message: '创建成功',
        data: newContest
      }
    }
  },
  // 修改大赛
  {
    url: '/dev-api/competition/update',
    method: 'post',
    response: (req) => {
      const index = contestList.findIndex(item => item.id === req.body.id)
      if (index >= 0) {
        contestList[index] = { ...contestList[index], ...req.body }
        return {
          code: 200,
          message: '修改成功',
          data: contestList[index]
        }
      }
      return {
        code: 500,
        message: '大赛不存在',
        data: null
      }
    }
  },
  // 删除大赛
  {
    url: '/dev-api/competition/delete',
    method: 'post',
    response: (req) => {
      const { id } = req.body
      const index = contestList.findIndex(item => item.id === id)
      if (index >= 0) {
        contestList.splice(index, 1)
        return {
          code: 200,
          message: '删除成功',
          data: null
        }
      }
      return {
        code: 500,
        message: '大赛不存在',
        data: null
      }
    }
  },
  // 获取大赛基本信息 (新增接口)
  {
    url: '/competition/basic-info',
    method: 'get',
    response: (req) => {
      const { id } = req.query;
      const contest = contestList.find(item => item.id == id);
      if (!contest) {
        return {
          code: 404,
          message: '大赛不存在',
          data: null
        }
      }

      // 计算当前阶段
      const now = new Date();
      let currentStage = 'unstarted';
      contest.groups?.forEach(group => {
        const start = new Date(group.stageStart);
        const end = new Date(group.stageEnd);
        if (now >= start && now <= end) {
          currentStage = group.stage;
        }
      });

      return {
        code: 200,
        message: 'success',
        data: {
          competitionName: contest.contestName,
          contestType: contest.contestType,
          status: contest.status,
          startTime: contest.startTime,
          endTime: contest.endTime,
          location: contest.location,
          organizer: contest.organizer,
          currentStage: currentStage
        }
      }
    }
  }
] as MockMethod[]
